package com.jron.cqiu.kernel.implement;

import com.jron.cqiu.kernel.persistence.IPersistence;
import com.jron.cqiu.Constants;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-18
 * @Time: 13:42:53
 */
public class SqlitePersistence implements IPersistence {

    private static Log log = LogFactory.getLog(SqlitePersistence.class);

    private boolean isCloseConnection = true;

    public Connection getConnection() {
        return getConnection(Constants.DEFAULT_DATASOURCE_URL);
    }

    public Connection getConnection(String url) {
        return getConnection(url, Constants.DEFAULT_DATASOURCE_DRIVER);
    }

    public Connection getConnection(String url, String driver) {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url);
        } catch (Exception e) {
            log.warn(e.getMessage());
        }
        return null;
    }

    public Connection getConnection(String url, String driver, String user, String password) {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            log.warn(e.getMessage());
        }
        return null;
    }

    public void closeQuietly(Connection connection) {
        DbUtils.closeQuietly(connection);
    }

    public void close(Connection connection) throws SQLException {
        DbUtils.close(connection);
    }

    public void commitAndCloseQuietly(Connection connection) {
        DbUtils.commitAndCloseQuietly(connection);
    }

    public void commitAndClose(Connection connection) throws SQLException {
        DbUtils.commitAndClose(connection);
    }

    public List query(String sql) {
        return query(sql, getConnection());
    }

    public List query(String sql, Connection connection) {
        return query(sql, connection, (Object[]) null);
    }

    public List query(String sql, Object... params) {
        return query(sql, getConnection(), params);
    }

    public List query(String sql, Connection connection, Object... params) {
        List result = null;
        try {
            QueryRunner qr = new QueryRunner();
            ResultSetHandler rsh = new MapListHandler();
            result = (List) qr.query(connection, sql, params, rsh);
        } catch (Exception ex) {
            log.warn(ex.getMessage());
        } finally {
            if (isCloseConnection) closeQuietly(connection);
        }
        return result;
    }

    public boolean update(String sql) {
        return update(sql, getConnection());
    }

    public boolean update(String sql, Connection connection) {
        return update(sql, connection, (Object[]) null);
    }

    /**
     * 更改 删除 新增操作
     *
     * @param sql
     * @param params
     * @return
     */
    public boolean update(String sql, Object... params) {
        return update(sql, getConnection(), params);
    }

    public boolean update(String sql, Connection connection, Object... params) {
        boolean flag = false;
        try {
            QueryRunner qRunner = new QueryRunner();
            int i = qRunner.update(connection, sql, params);
            if (i > 0) flag = true;
        } catch (SQLException e) {
            log.warn(e.getMessage());
        } finally {
            if (isCloseConnection) commitAndCloseQuietly(connection);
        }
        return flag;
    }

    /**
     * 返回当前 (每次数据库操作后是否保持数据库连接的) 设定
     *
     * @return
     */
    public boolean checkConnectionSet() {
        return isCloseConnection;
    }

    /**
     * 每次数据库操作后保持数据库连接 默认为关闭
     *
     * @param closeConnection
     */
    public void keepConnection() {
        isCloseConnection = false;
    }

    /**
     * 还原默认值 每次数据库操作后是否保持数据库连接 (默认为关闭)
     *
     * @param closeConnection
     */
    public void keepClosingConnection() {
        isCloseConnection = true;
    }
}
